package com.ja.openchat.ui.chat.adapter.viewholder;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import androidx.core.util.Pair;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.ja.openchat.R;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.file.DocumentInfo;
import com.zy.openchat.core.model.message.MessageContent;
import com.zy.openchat.core.model.message.MessageInfo;
import com.zy.openchat.core.model.message.UiMessage;
import com.zy.openchat.core.model.sticker.StickerInfo;
import com.zy.openchat.core.model.video.VideoInfo;
import com.ja.openchat.ui.chat.ConversationFragment;
import com.ja.openchat.ui.chat.annotation.EnableContextMenu;
import com.ja.openchat.ui.chat.annotation.MessageContentType;
import com.ja.openchat.ui.chat.annotation.MessageContextMenuItem;
import com.zy.openchat.Constants;
import com.zy.openchat.util.WeChatImageUtils;
import com.ja.openchat.views.BubbleImageView;
import com.ja.openchat.views.FullVideoView;
import com.lqr.emoji.StickerManager;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 图片尺寸展示高仿微信
 * 占位图-缩略图-原图
 */
@MessageContentType(value = TelegramManager.MessageType.MessageType_Animation)
@EnableContextMenu
public class AnimationMessageContentViewHolder extends MediaMessageContentViewHolder {

    private static final String TAG = "ImageMessageContentView";
    @BindView(R.id.imageView)
    BubbleImageView imageView;
    @BindView(R.id.innerVideo)
    FullVideoView innerVideo;
    @BindView(R.id.innerImage)
    ImageView innerImage;
    @BindView(R.id.playImageView)
    View playImageView;

    private String imagePath;

    public AnimationMessageContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
        super(fragment, adapter, itemView);
    }

    @Override
    public void onBind(UiMessage message) {
        MessageContent videoMessageContent =  message.message.content;
        if(videoMessageContent.animation==null){
            return;
        }
        int width = Constants.MEDIA_MAX_WIDTH;
        int height = Constants.MEDIA_MAX_WIDTH;
        if(videoMessageContent.animation!=null){
            VideoInfo videoInfo = message.message.content.animation;
            int imageSize[] = WeChatImageUtils.getImageSizeByOrgSizeToWeChat((int) videoInfo.width, (int) videoInfo.height);
            width = imageSize[0] > 0 ? imageSize[0] : Constants.MEDIA_MAX_WIDTH;
            height = imageSize[1] > 0 ? imageSize[1] : Constants.MEDIA_MAX_WIDTH;
        }
        imageView.getLayoutParams().width = width;
        imageView.getLayoutParams().height = height;
        if (videoMessageContent.animation.isAnimationDownloaded()) {
            imagePath = videoMessageContent.animation.localAnimationPath();
        }else{
            TelegramManager.getTelegramManager().downloadFile(message.message.chat_id+"_" + message.message.id, message.message.getAnimationFileId(), 0, TelegramManager.FileType.FileType_Message_Animation);
        }
        loadMediaAsGif(videoMessageContent.animation.isAnimationDownloaded(),imagePath,innerVideo, innerImage,playImageView);


    }

    @OnClick(R.id.imageView)
    void preview() {
        previewMM();
    }

    @OnClick(R.id.playImageView)
    void play() {
        MessageContent videoMessageContent =  message.message.content;
        if(videoMessageContent.animation==null){
            return;
        }
        if(!videoMessageContent.animation.isAnimationDownloaded()){
            return;
        }
        if(DocumentInfo.isGifeFile(videoMessageContent.animation.localAnimationPath())){
            innerImage.setVisibility(View.VISIBLE);
            innerVideo.setVisibility(View.INVISIBLE);
            return;
        }
        if(innerVideo.isPlaying()){
            message.isPlaying = false;
            innerImage.setVisibility(View.VISIBLE);
            innerVideo.setVisibility(View.INVISIBLE);
            playImageView.setVisibility(View.VISIBLE);
            innerVideo.pause();
        }else{
            message.isPlaying = true;
            innerImage.setVisibility(View.INVISIBLE);
            innerVideo.setVisibility(View.VISIBLE);
            playImageView.setVisibility(View.GONE);
            innerVideo.setTag(110);
            innerVideo.setVideoPath(videoMessageContent.animation.localAnimationPath());
            innerVideo.start();
        }

    }

    @Override
    protected void setSendStatus(MessageInfo item) {
        super.setSendStatus(item);
        if (item.messageType == TelegramManager.MessageType.MessageType_Animation) {
            if (item.isOutgoing()) {
                TelegramManager.MessageSendState sentStatus = item.sendState();
                if (sentStatus == TelegramManager.MessageSendState.MessageSendState_Pending) {
                    imageView.setPercent(message.progress);
                    imageView.setProgressVisible(true);
                    imageView.showShadow(true);
                } else if (sentStatus == TelegramManager.MessageSendState.MessageSendState_Fail) {
                    imageView.setProgressVisible(false);
                    imageView.showShadow(false);
                } else if (sentStatus == TelegramManager.MessageSendState.MessageSendState_Success) {
                    imageView.setProgressVisible(false);
                    imageView.showShadow(false);
                }
            } else {
                imageView.setProgressVisible(false);
                imageView.showShadow(false);
            }
        }
    }

    @MessageContextMenuItem(tag = MessageContextMenuItemTags.TAG_ADD_STICKER, priority = 8)
    public void addSticker(View itemView, UiMessage message) {
        if(message!=null&&message.message!=null){
            StickerInfo stickerInfo = StickerManager.getInstance().hasStickerReturnInfo(message.message.getAnimationFileId());
//            if(stickerInfo!=null){
//                messageViewModel.removeSticker(stickerInfo);
//            }else{
            if(stickerInfo!=null){
                ToastUtils.showShort(fragment.getString(R.string.emoji_added));
                return;
            }
            messageViewModel.addSticker(message.message.getRemoteAnimationFileId());
//            }

        }
    }


    @Override
    public Pair<String,Integer> contextMenuTitle(Context context, String tag) {
        if (MessageContextMenuItemTags.TAG_ADD_STICKER.equals(tag)) {
//            boolean isHave = false;
//            if(message!=null&&message.message!=null){
//                isHave = StickerManager.getInstance().hasSticker(message.message.getAnimationFileId());
//            }
//            return new Pair<>(isHave?"取消表情":"添加表情",R.drawable.menu_copy);
            return new Pair<>(context.getString(R.string.add_emoji),R.mipmap.menu_add_emoji);
        }
        return super.contextMenuTitle(context, tag);
    }

}
